package models

import (
	orm "go-papers/database"
	"go-papers/tools"
	"strconv"
	_ "time"
)

type MemberStudent struct {
	StudentId    int    `json:"studentId" gorm:"type:int(11) unsigned;primary_key"` //
	MemberId     int    `json:"memberId" gorm:"type:int(11) unsigned;"`             // 会员id
	StudentName  string `json:"studentName" gorm:"type:varchar(64);"`               // 姓名
	StudentDay   string `json:"studentDay" gorm:"type:varchar(255);"`               // 出生年月
	StudentSex   string `json:"studentSex" gorm:"type:varchar(16);"`                // 出生年月
	StudentAge   string `json:"studentAge" gorm:"type:varchar(32);"`                // 年龄
	StudentPhone string `json:"studentPhone" gorm:"type:varchar(32);"`              // 手机
	StudentGrade string `json:"studentGrade" gorm:"type:varchar(255);"`             // 年级
	IsDefault    string `json:"isDefault" gorm:"type:varchar(32);"`                 // 是否默认 1 是，0 否
	Province     string `json:"province" gorm:"type:varchar(32);"`                  // 省份
	City         string `json:"city" gorm:"type:varchar(32);"`                      // 城市
	Area         string `json:"area" gorm:"type:varchar(32);"`                      // 区域
	Address      string `json:"address" gorm:"type:varchar(255);"`                  // 地址
	AddressTxt   string `json:"addressTxt" gorm:"type:text;"`                       // 地址 json
	CreateBy     string `json:"createBy" gorm:"type:varchar(255);"`                 // 创建
	UpdateBy     string `json:"updateBy" gorm:"type:varchar(255);"`                 // 跟新
	DataScope    string `json:"dataScope" gorm:"-"`
	Params       string `json:"params"  gorm:"-"`
	BaseModel
}

func (MemberStudent) TableName() string {
	return "member_student"
}

// 创建MemberStudent
func (e *MemberStudent) Create() (MemberStudent, error) {
	var doc MemberStudent
	table := orm.Eloquent.Table(e.TableName())
	result := table.Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e

	if doc.IsDefault == "1" {
		table2 := orm.Eloquent.Table(e.TableName())
		table2.Where("member_id = ? and student_id != ?", doc.MemberId, doc.StudentId).Update("is_default", "0")
	}
	return doc, nil
}

// 获取MemberStudent
func (e *MemberStudent) GetByID(sid int) (MemberStudent, error) {
	var doc MemberStudent
	table := orm.Eloquent.Table(e.TableName())
	table = table.Where("student_id = ?", sid)
	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 获取MemberStudent
func (e *MemberStudent) Get() (MemberStudent, error) {
	var doc MemberStudent
	table := orm.Eloquent.Table(e.TableName())
	if e.StudentId != 0 {
		table = table.Where("student_id = ?", e.StudentId)
	}
	if e.MemberId != 0 {
		table = table.Where("member_id = ?", e.MemberId)
	}
	if e.IsDefault != "" {
		table = table.Where("is_default = ?", e.IsDefault)
	}
	if e.StudentName != "" {
		table = table.Where("student_name = ?", e.StudentName)
	}
	if e.StudentDay != "" {
		table = table.Where("student_day = ?", e.StudentDay)
	}
	if e.StudentAge != "" {
		table = table.Where("student_age = ?", e.StudentAge)
	}
	if e.StudentPhone != "" {
		table = table.Where("student_phone = ?", e.StudentPhone)
	}
	if e.StudentGrade != "" {
		table = table.Where("student_grade = ?", e.StudentGrade)
	}
	if e.Province != "" {
		table = table.Where("province = ?", e.Province)
	}
	if e.City != "" {
		table = table.Where("city = ?", e.City)
	}
	if e.Area != "" {
		table = table.Where("area = ?", e.Area)
	}
	if e.Address != "" {
		table = table.Where("address = ?", e.Address)
	}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

func (e *MemberStudent) GetSearchPage(pageSize int, pageIndex int) ([]MemberStudent, int, error) {
	var doc []MemberStudent

	table := orm.Eloquent.Select("*").Table(e.TableName())
	if e.StudentId != 0 {
		table = table.Where("student_id = ?", e.StudentId)
	}
	if e.MemberId != 0 {
		table = table.Where("member_id = ?", e.MemberId)
	}
	if e.IsDefault != "" {
		table = table.Where("is_default = ?", e.IsDefault)
	}
	if e.StudentName != "" {
		table = table.Where("student_name = ?", e.StudentName)
	}
	if e.StudentDay != "" {
		table = table.Where("student_day = ?", e.StudentDay)
	}
	if e.StudentAge != "" {
		table = table.Where("student_age = ?", e.StudentAge)
	}
	if e.StudentPhone != "" {
		table = table.Where("student_phone = ?", e.StudentPhone)
	}
	if e.StudentGrade != "" {
		table = table.Where("student_grade = ?", e.StudentGrade)
	}
	if e.Province != "" {
		table = table.Where("province = ?", e.Province)
	}
	if e.City != "" {
		table = table.Where("city = ?", e.City)
	}
	if e.Area != "" {
		table = table.Where("area = ?", e.Area)
	}
	if e.Address != "" {
		table = table.Where("address = ?", e.Address)
	}

	var count int

	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

// 获取MemberStudent带分页
func (e *MemberStudent) GetPage(pageSize int, pageIndex int) ([]MemberStudent, int, error) {
	var doc []MemberStudent

	table := orm.Eloquent.Select("*").Table(e.TableName())
	if e.StudentId != 0 {
		table = table.Where("student_id = ?", e.StudentId)
	}
	if e.MemberId != 0 {
		table = table.Where("member_id = ?", e.MemberId)
	}
	if e.IsDefault != "" {
		table = table.Where("is_default = ?", e.IsDefault)
	}
	if e.StudentName != "" {
		table = table.Where("student_name = ?", e.StudentName)
	}
	if e.StudentDay != "" {
		table = table.Where("student_day = ?", e.StudentDay)
	}
	if e.StudentAge != "" {
		table = table.Where("student_age = ?", e.StudentAge)
	}
	if e.StudentPhone != "" {
		table = table.Where("student_phone = ?", e.StudentPhone)
	}
	if e.StudentGrade != "" {
		table = table.Where("student_grade = ?", e.StudentGrade)
	}
	if e.Province != "" {
		table = table.Where("province = ?", e.Province)
	}
	if e.City != "" {
		table = table.Where("city = ?", e.City)
	}
	if e.Area != "" {
		table = table.Where("area = ?", e.Area)
	}
	if e.Address != "" {
		table = table.Where("address = ?", e.Address)
	}

	// 数据权限控制(如果不需要数据权限请将此处去掉)
	dataPermission := new(DataPermission)
	dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
	table, err := dataPermission.GetDataScope(e.TableName(), table)
	if err != nil {
		return nil, 0, err
	}
	var count int

	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

// 更新MemberStudent
func (e *MemberStudent) Update(id int) (update MemberStudent, err error) {
	table := orm.Eloquent.Table(e.TableName())
	if err = table.Where("student_id = ?", id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}

	if update.IsDefault == "1" {
		table2 := orm.Eloquent.Table(e.TableName())
		table2.Where("member_id = ? and student_id != ?", update.MemberId, update.StudentId).Update("is_default", "0")
	}

	return
}

func (e *MemberStudent) UpdateUserDefault(id string) (success bool, err error) {
	table := orm.Eloquent.Table(e.TableName())
	sql := " UPDATE member_student SET is_default = 0 WHERE member_id = " + strconv.Itoa(e.MemberId) + " "
	if err = table.Exec(sql).Error; err != nil {
		return false, err
	}

	// 指定设置为 1
	sql2 := " UPDATE member_student SET is_default = 1 WHERE member_id = " + strconv.Itoa(e.MemberId) + " AND student_id = " + id + " "
	if err = table.Exec(sql2).Error; err != nil {
		return false, err
	}

	return true, err

}

// 删除MemberStudent
func (e *MemberStudent) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("student_id = ?", id).Delete(&MemberStudent{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

//批量删除
func (e *MemberStudent) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("student_id in (?)", id).Delete(&MemberStudent{}).Error; err != nil {
		return
	}
	Result = true
	return
}
